﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace RLZY
{
    public partial class FormCJJKB : Form
    {
        public FormCJJKB()
        {
            InitializeComponent();
            
        }
        
        private static FormCJJKB childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormCJJKB ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormCJJKB();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }
       
        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            dtpDateStart.Text = Convert.ToString(dt.AddDays(1 - dt.Day)); 
        }

     

        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        private void cmbBZ_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        
       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

        

        /// <summary>
        /// Toggles the buttons used for exporting the grid data.
        /// </summary>
        /// <param name="enabled">Boolean, used to signify whether or not to enable/disable the export buttons.</param>
        



        public string LastDate { get; set; }

        private void FormNT_Show_FormClosing(object sender, FormClosingEventArgs e)
        {
            others.RYFB fb = new RYFB();
            fb.DropGZ1();
        }

        

        private void btn测奖金空表_Click(object sender, EventArgs e)
        {
            try
            {
                others.SJ sj = new SJ();
                sj.DropGZ1();
                sj.cjjkb(this.dtpDateStart.Text, dtpDateEnd.Text);
                DataSet ds = sj.Getcjjkb();
                this.BindData(ds);
                sj.DropGZ1();
            }
            catch (Exception ex) { LOG.Error(ex); }

        }
        private void ExportToggle(bool enabled)
        {
            btn2Excel.Enabled = enabled;
            //btnExportPDF.Enabled = enabled;
            //btnExportWord.Enabled = enabled;
            //btnCancelExport.Enabled = !enabled;
        }

        private void btn2Excel_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            string filename = "测奖金空表_导出" + dt.ToFileTime().ToString();//127756416859912816
            StringBuilder strSql = new StringBuilder();
            strSql.Append("D:\\");
            strSql.Append(filename);
            strSql.Append(".xls");
            string path = strSql.ToString();  //测试一个xls文档
            //ultraGridExcelExporter1.Export(ultraGrid1, path);
            //System.Diagnostics.Process.Start("explorer.exe", path);
            ////System.Diagnostics.Process.Start(path); //打开此文件。
            // Retrieve all running processes on the local computer.
            Process[] processlist = Process.GetProcesses();

            // Iterating through the running processses, and Killing the process that is holding your previosly opened Excel file.
            foreach (Process runningProcess in processlist)
            {
                if (runningProcess.MainWindowTitle.Contains(path))
                    runningProcess.Kill();
            }

            ExportToggle(false);
            try
            {
                ultraGridExcelExporter1.ExportAsync(ultraGrid1, path);
                //System.Diagnostics.Process.Start("explorer.exe", path);
                ExportToggle(true);
                //Msg.DisplayMsg("", "导出文件'" + path + "'", 1200);
                Msg.DisplayMsg("提示 ", "导出文件在：'" + path + "'", 6000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ExportToggle(true);
            }
        }
        

        
        
       
    }
}
